﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Settings;
using Utils;
using System.Xml;

namespace SMSROUTER.Business
{
    public partial class SmsInWaiting
    {
        public static List<SmsInWaiting> GetForRouter(List<int> routerIds)
        {
            List<SmsInWaiting> retList;
            if (routerIds == null || routerIds.Count == 0) return new List<SmsInWaiting>();
            try
            {                
                //Khai báo parameter
                SqlParameter param;
                using (DataTable dtParams = new DataTable())
                {
                    dtParams.Columns.Add("TypeName", typeof(long));
                    foreach (var item in routerIds) dtParams.Rows.Add(item);
                    param = new SqlParameter("@RouterIds", dtParams);
                    param.SqlDbType = SqlDbType.Structured;
                    param.TypeName = "BigIntTableType";
                }
                using (SqlConnection conn = AppSetting.GetConnection())
                {
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        using (SqlDataReader rd = SqlHelper.ExecuteReader(trans, CommandType.StoredProcedure, "usp_SmsInWaiting_GetForRouter", param))
                        {
                            retList = ObjectClass.CreateList<SmsInWaiting>(rd);
                        }
                        trans.Commit();
                    }
                    return retList;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public static void BulkInsert(IList<SmsInWaiting> source)
        {
            if (source.Count > 0)
            {
                try
                {
                    DataAccess.BulkInsert(source, "SmsInWaiting");
                }
                catch (Exception ex)
                {
                    Log.InsertLog(ex, null, AppSetting.WarningType.mo_error, null);
                    try
                    {
                        Xml.WriteListToFile<SmsInWaiting>(source, AppSetting.SmsInWaitingBackupFileName);
                    }
                    catch (Exception ex1)
                    {
                        Log.InsertLog("SmsInWaiting Write to Xml has fail|" + ex1.Message, ex1.StackTrace, ex1.Source, AppSetting.WarningType.mo_error, null);
                        //throw new Exception();
                    }
                }
            }
        }
    }
}
